<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买人" prop="buyNickName">
        <el-input
          v-model="queryParams.buyNickName"
          placeholder="请输入购买人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定人" prop="bindNickName">
        <el-input
          v-model="queryParams.bindNickName"
          placeholder="请输入绑定人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="苏学码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入苏学码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已绑定" prop="hasBind">
        <el-select v-model="queryParams.hasBind" placeholder="请选择是否已绑定课程" clearable>
          <el-option
            v-for="dict in dict.type.ljsf"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="hasValid">
        <el-select v-model="queryParams.hasValid" placeholder="请选择苏学码状态" clearable>
          <el-option
            v-for="dict in dict.type.sxmzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="购买时间">
        <el-date-picker
          v-model="daterangeBuyTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="绑定时间">
        <el-date-picker
          v-model="daterangeBindTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="失效时间">
        <el-date-picker
          v-model="daterangeValidTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:userPlatform:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:userPlatform:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:userPlatform:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:userPlatform:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="batchValid"
          v-hasPermi="['business:userPlatform:batchValid']"
        >批量失效</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userPlatformList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="baseId" v-if="false"/>
      <el-table-column label="课程名称" align="center" prop="platformName" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="苏学码" align="center" prop="code" />
      <el-table-column label="购买人" align="center" prop="buyNickName" />
      <el-table-column label="购买时间" align="center" prop="buyTime" width="180">
        <template slot-scope="scope">
          <!--<span>{{ parseTime(scope.row.buyTime, '{y}-{m}-{d}') }}</span>-->
          <span>{{ scope.row.buyTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已绑定课程" align="center" prop="hasBind">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ljsf" :value="scope.row.hasBind"/>
        </template>
      </el-table-column>
      <el-table-column label="绑定人" align="center" prop="bindNickName">
        <template slot-scope="scope">
          <span>{{ scope.row.bindNickName ? scope.row.bindNickName : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定时间" align="center" prop="bindTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.bindTime ? scope.row.bindTime : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="苏学码状态" align="center" prop="hasValid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sxmzt" :value="scope.row.hasValid"/>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="validTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.validTime ? scope.row.validTime : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:userPlatform:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:userPlatform:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改苏学码管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程id" prop="platformId">
          <el-input v-model="form.platformId" placeholder="请输入课程id" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="购买人" prop="buyUserName">
          <el-input v-model="form.buyUserName" placeholder="请输入购买人" />
        </el-form-item>
        <el-form-item label="绑定人" prop="bindUserName">
          <el-input v-model="form.bindUserName" placeholder="请输入绑定人" />
        </el-form-item>
        <el-form-item label="苏学码" prop="code">
          <el-input v-model="form.code" placeholder="请输入苏学码" />
        </el-form-item>
        <el-form-item label="是否已绑定" prop="hasBind">
          <el-select v-model="form.hasBind" placeholder="请选择是否已绑定">
            <el-option
              v-for="dict in dict.type.ljsf"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效状态" prop="hasValid">
          <el-select v-model="form.hasValid" placeholder="请选择生效状态">
            <el-option
              v-for="dict in dict.type.ljsf"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="失效时间" prop="validTime">
          <el-date-picker clearable
                          v-model="form.validTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择失效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买时间" prop="buyTime">
          <el-date-picker clearable
                          v-model="form.buyTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="绑定时间" prop="bindTime">
          <el-date-picker clearable
                          v-model="form.bindTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择绑定时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserPlatform, getUserPlatform, batchValid, addUserPlatform, updateUserPlatform } from "@/api/business/userPlatform";

export default {
  name: "UserPlatform",
  dicts: ['ljsf', 'sxmzt'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 苏学码管理表格数据
      userPlatformList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 绑定时间时间范围
      daterangeValidTime: [],
      // 绑定时间时间范围
      daterangeBuyTime: [],
      // 绑定时间时间范围
      daterangeBindTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        buyUserName: undefined,
        buyNickName: undefined,
        bindUserName: undefined,
        bindNickName: undefined,
        code: undefined,
        hasBind: undefined,
        hasValid: undefined,
        validTime: undefined,
        buyTime: undefined,
        bindTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        baseId: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        platformId: [
          { required: true, message: "课程id不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        buyUserName: [
          { required: true, message: "购买人不能为空", trigger: "blur" }
        ],
        bindUserName: [
          { required: true, message: "绑定人不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "苏学码不能为空", trigger: "blur" }
        ],
        hasBind: [
          { required: true, message: "是否已绑定不能为空", trigger: "change" }
        ],
        hasValid: [
          { required: true, message: "生效状态不能为空", trigger: "change" }
        ],
        validTime: [
          { required: true, message: "失效时间不能为空", trigger: "blur" }
        ],
        buyTime: [
          { required: true, message: "购买时间不能为空", trigger: "blur" }
        ],
        bindTime: [
          { required: true, message: "绑定时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询苏学码管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeValidTime && '' != this.daterangeValidTime) {
        this.queryParams.params["beginValidTime"] = this.daterangeValidTime[0];
        this.queryParams.params["endValidTime"] = this.daterangeValidTime[1];
      }
      if (null != this.daterangeBuyTime && '' != this.daterangeBuyTime) {
        this.queryParams.params["beginBuyTime"] = this.daterangeBuyTime[0];
        this.queryParams.params["endBuyTime"] = this.daterangeBuyTime[1];
      }
      if (null != this.daterangeBindTime && '' != this.daterangeBindTime) {
        this.queryParams.params["beginBindTime"] = this.daterangeBindTime[0];
        this.queryParams.params["endBindTime"] = this.daterangeBindTime[1];
      }
      listUserPlatform(this.queryParams).then(response => {
        this.userPlatformList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        baseId: undefined,
        platformId: undefined,
        orderNo: undefined,
        buyUserName: undefined,
        bindUserName: undefined,
        code: undefined,
        hasBind: undefined,
        hasValid: undefined,
        validTime: undefined,
        buyTime: undefined,
        bindTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeValidTime = [];
      this.daterangeBuyTime = [];
      this.daterangeBindTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.baseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加苏学码管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const baseId = row.baseId || this.ids
      getUserPlatform(baseId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改苏学码管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.baseId != null) {
            updateUserPlatform(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUserPlatform(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 批量失效 */
    batchValid(row) {
      const baseIds = row.baseId || this.ids;
      this.$modal.confirm('是否确认批量失效苏学码？注意：该操作不可逆转！').then(() => {
        this.loading = true;
        return batchValid(baseIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/userPlatform/export', {
        ...this.queryParams
      }, `userPlatform_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
